package com.suray.wcs.service.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.suray.commin.core.redis.RedisCache;
import com.suray.commin.core.utils.Constants;
import com.suray.commin.core.utils.Convert;
import com.suray.commin.core.utils.StringUtils;
import com.suray.wcs.service.system.entity.SysConfig;
import com.suray.wcs.service.system.entity.SysConfigDB;
import com.suray.wcs.service.system.mapper.SysConfigMapper;
import com.suray.wcs.service.system.service.ISysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 配置服务类
 * @author caox@suray.cn
 * @date 2020/7/21 16:33
 */
@Service
public class SysConfigServiceImpl extends ServiceImpl<SysConfigMapper, SysConfigDB> implements ISysConfigService {
    @Autowired
    private RedisCache redisCache;

    @Autowired
    private SysConfigMapper configMapper;
    /**
     * @description:获取配置
     * [type] @return com.suray.wcs.service.system.entity.SysConfigdb
     * @author caox@suray.cn
     * @date 2020/7/21 16:33
     */
    @Override
    public SysConfigDB getSysConfigdb(String type) {
        QueryWrapper<SysConfigDB> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysConfigDB::getType, type).eq(SysConfigDB::getIsDelete,"0");
        return getOne(queryWrapper);
    }

    @Override
    public String selectConfigByKey(String configKey) {
        String configValue = Convert.toStr(redisCache.getCacheObject(getCacheKey(configKey)));
        if (StringUtils.isNotEmpty(configValue))
        {
            return configValue;
        }
        SysConfig config = new SysConfig();
        config.setConfigKey(configKey);
        SysConfig retConfig = configMapper.selectConfig(config);
        if (StringUtils.isNotNull(retConfig))
        {
            redisCache.setCacheObject(getCacheKey(configKey), retConfig.getConfigValue());
            return retConfig.getConfigValue();
        }
        return StringUtils.EMPTY;
    }

    /**
     * 设置cache key
     *
     * @param configKey 参数键
     * @return 缓存键key
     */
    private String getCacheKey(String configKey)
    {
        return Constants.SYS_CONFIG_KEY + configKey;
    }
}
